extends KinematicBody2D

var v=Vector2(0,0)
var g=980
var jump=500
var climb_speed=70
var speed=100
var u=0.01
var movable=false
var items=[]
var if_bag=false
var gui_turns=0
var can_climb=false

func _physics_process(delta):
	if !movable:
		return
	
	if!can_climb:
		v.y+=g*delta
	if i.keys.run:
		speed=200
	else:
		speed=100
	if(!(i.keys.right&&i.keys.left)):
		if(i.keys.left):
			v.x=-speed
		elif(i.keys.right):
			v.x=speed
		elif(is_on_floor()||is_on_ceiling())&&!can_climb:
			v.x=lerp(v.x,0,1-pow(u,delta))
		elif can_climb:
			v.x=0
	elif(is_on_floor()||is_on_ceiling())&&!can_climb:
		v.x=lerp(v.x,0,1-pow(u,delta))
	elif can_climb:
		v.x=0
	if(i.keys.jump&&is_on_floor()):
		v.y=-jump
	if(can_climb):
		if(i.keys.jump):
			v.y=-climb_speed
		elif(i.keys.down):
			v.y=climb_speed
		else:
			v.y=0
	
	v=move_and_slide(v,Vector2.UP)
	if is_on_floor():
		v.y=0
	if is_on_ceiling():
		v.y=max(v.y,0)

func on_area_entered(area):
	match area.name:
		"ladder0":
			can_climb=true
		"umbrella_long":
			items.append([sys.items.umbrella_long,1])
			get_parent().remove_child(area.get_parent())
		"bag":
			if_bag=true
			get_parent().remove_child(area.get_parent())
		"gui1":
			position=Vector2(position.x+8*64,position.y)
			sys.camera.position=Vector2(sys.camera.position.x+8*64,sys.camera.position.y)
			gui_turns+=1

func on_area_exited(area):
	match area.name:
		"ladder0":
			can_climb=false
